﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Configuration;

namespace HIS.Controllers.FrontOffice {
    public class LobSelection : HIS.Controllers.AppBaseController {
        private string strAction = string.Empty;
        private string strNotification = string.Empty;

        #region Constructors
        public LobSelection() {
            Data.Add("Pagetitle", "Lob Selection");
            Data.Add("Notification", strNotification);
        }
        #endregion
        [HIS.Utils.Securities.Authorization(RoleAction = "fo-registration")]
        public override void Index() {
            try {
                string redirectUri = HttpContext.Current.Request["redirectUri"];
                Data.Add("redirectUri", redirectUri);
                //Data.Add("DdLobId", GetLob("DdLobId", base.RestService, null).NullSafeToString());
                Data.Add("DdLobId", GetHuLobRel("DdLobId", base.RestService, null));
                Data.Add("Actionform", Data["rooturl"] + "frontoffice/lobselection/lobpost");
                RenderFile("frontoffice\\lobselection.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [HIS.Utils.Securities.Authorization(RoleAction = "fo-registration")]
        public void LobPost() {
            try {
                string redirectUri = HttpContext.Current.Request["redirectUri"];
                string Lob = HttpContext.Current.Request["DdLobId"];
                if (!string.IsNullOrEmpty(redirectUri)) {
                    //add to session
                    HttpContext.Current.Session.Add("Lob", Lob);
                    GoToUrl(Data["rooturl"] + redirectUri);
                }

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }


        [HIS.Utils.Securities.Authorization(RoleAction = "all")]
        public string GetHuLobRel(string name, KeanuMvc.Reference.ClientRestService clientService, string Selected) {
            string strResult = string.Empty;

            string strUri = Global.ServiceUriDictionary["lob"] +  Global.ServiceUriDictionary["requestformat"];
            StringReader stringReader = clientService.GetJson(strUri);
            var b = new[] { new { Name = string.Empty, Value = 0 } };
            b = JsonConvert.DeserializeAnonymousType(stringReader.ReadToEnd(), b);
            
            List<object[]> listDropdown = new List<object[]>();
            listDropdown.Add(new object[] { "", "--Select--" });
            foreach (var item in b) {
                listDropdown.Add(new object[] { item.Value,item.Name});

            }

            strResult = KeanuMvc.Reference.Html.DropDown(name, "class='required'", listDropdown, Selected);
            listDropdown.Clear();
            listDropdown = null;
            stringReader.Dispose();
            return strResult;
        }
    }
}